Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    isLogin: false,
    isCollapsed: false,
    menu_index: null,
    user_name: '',
    user_id: '',
  },
  getters: {
    isLogin: state => state.isLogin,
    // isCollapsed: state => state.isCollapsed,
    menu_index: state => {
      return state.menu_index || localStorage.getItem('menu_index')
    },
    user_id: state => state.user_id || localStorage.getItem('user_id'),
    user_name: state => state.user_name || localStorage.getItem('user_name')
  },
  mutations: {
    userStatus (state, flag) {
      state.isLogin = flag
    },
    userUpdate (state, name) {
      localStorage.setItem('user_name', name)
      state.user_name = name
    },
    userIdUpdate (state, id) {
      localStorage.setItem('user_id', id)
      state.user_id = id
    },
    updateCollapse (state, isCollapse) {
      state.isCollapsed = isCollapse
    },
    updateMenuIndex (state, menuIndex) {
      if (!state.menu_index) {
        state.menu_index = localStorage.getItem('menu_index')
      } else {
        state.menu_index = menuIndex
      }
      localStorage.setItem('menu_index', state.menu_index)
    }
  },
  actions: {
    userLogin ({commit}, flag) {
      commit('userStatus', flag)
    },
    userRecord ({commit}, name) {
      commit('userUpdate', name)
    },
    userIdRecord ({commit}, id) {
      commit('userIdUpdate', id)
    },
    userLogout ({commit}, flag) {
      commit('userStatus', flag)
    },
    setCollapse ({commit}, is_collapse) {
      commit('updateCollapse', is_collapse)
    },
    setMenuIndex ({commit}, menu_index) {
      commit('updateMenuIndex', menu_index)
    },
  }
})
